/**
 * 
 */
package com.atung.tool;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.validator.internal.util.privilegedactions.NewProxyInstance;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atung.model.Content;
import com.atung.model.SeasonEnum;
import com.atung.model.SendData;
import com.atung.model.TemplateMessage;
//import com.atung.wxpay.WXConfig;
import com.atung.tool.wxUrl.WxUrlTool;

import cn.hutool.http.HttpUtil;


/**
 * 
 * @author lc <br>
 * 2020年11月18日 下午3:38:36
 */
public class SendTemplateTool {
    
    private static String fache = "vscEaPULLPt_t2FZIdvY4GChS07pK3yscrtAK_vnOj8";
    private static String daoda = "h5EMqgGLTzbOAXJT3wx3MKK3XeL_3hAOuhECa-imYX8";
    
    @Value("${wx.appid}")
    private static String appid;
    @Value("${wx.secret}")
    private static String secret;

    
    //调用发送消息模版
    public static void  sendTemplate(String openid,SeasonEnum type,Map<String,Object> map){
        //获取token 
//        String accessToken = getAccessToken();
        String accessToken=WxUrlTool.GetAccessToken.replace("APPID", appid).replace("SECRET", secret);
        System.out.println("accessToken =====" +accessToken);
        //拼接模版消息请求路径
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?" +
                "access_token=" + accessToken;
        System.out.println(url);
        //创建发送消息对象
        TemplateMessage mess = new TemplateMessage();
        mess.setTouser(openid);
//        mess.setPage(WXConfig.WECHAT_NOTIFY_URL_PC);
        Map<String, Content> data = new HashMap<String,Content>();
        //判断类型
        if (type == SeasonEnum.CAR) {
            mess.setTemplate_id(fache);
            //行程名称
            data.put("thing1", new Content("测试"));
            //行程出发地
            data.put("thing2", new Content("测试出发地"));
            //行程到达地
            data.put("thing3", new Content("测试目的地"));
            //出发时间
            data.put("date4", new Content("2020-07-11 15:00"));
            //订单号
            data.put("character_string6", new Content("测试00001"));
        }else if(type == SeasonEnum.SENDCAR){
            mess.setTemplate_id(daoda);
            //上车地点
            data.put("thing5", new Content("上车地点"));
            //目的地
            data.put("thing6", new Content("目的地"));
            //出发时间
            data.put("date1", new Content("2020-07-11 15:00"));
            //车牌号
            data.put("car_number2", new Content("阿cada"));
            //司机姓名
            data.put("name3", new Content("测试"));
        }
       
        mess.setData(data);
        String json = JSONObject.toJSONString(mess);
        String postData = HttpUtil.post(url, json);
        System.out.println(postData);
    }
    
    
//    public static String getAccessToken(){
//        RestTemplate restTemplate = new RestTemplate();
//        Map<String,String> map = new HashMap<>();
//        map.put("APPID", WXConfig.APPID);
//        map.put("APPSECRET", "0af31ce449aa2683336c004cfa211610");
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class,map);
//        String body = responseEntity.getBody();
//        JSONObject object = JSON.parseObject(body);
//        String string = object.getString("access_token");
//        return string;
//    }

}

